package in.muton.edushare.pages.publicpages.userpages;

import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserBatchLink;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.UserRoles;
import com.edushare.common.vo.Users;

public class UserRolePanel extends Panel {

	private static final long serialVersionUID = 6835930429889262458L;
	private IModel<Users> userModel;

	public UserRolePanel(String id, IModel<Users> model) {
		super(id, model);
		this.userModel = model;
		loadPanel();
	}

	private void loadPanel() {
		//1. University roles
		List<UserRoles> roles = userModel.getObject().getRoleList();
		UserRoles ur = null;
		boolean isUnivAdmin = false;
		boolean isDeptRoleExist = false;
		boolean isUnivRoleExist = false;
		
		if(roles!=null && roles.size()>0)
			ur = roles.get(0);
		if(ur!=null && ur.getStatus() != UserRoles.STATUS_INVITED){
			if(ur.getRoleType() == UserRoles.ADMIN_ROLE){
				isUnivAdmin = true;
			}
			isUnivRoleExist = true;
		}
		
		//2. department roles
		List<UserDeptLink> deptRoles = userModel.getObject().getDepartmentLinks();
		if(deptRoles.size()>0){
			for(UserDeptLink udl: deptRoles){
				if(udl.getStatus() != UserDeptLink.STATUS_INVITED){
					isDeptRoleExist = true;
					break;
				}
			}
			
		}
		
		//3. format list
		List<Fragment> allRoles = new ArrayList<Fragment>();
		
		if(isUnivAdmin || (!isDeptRoleExist && isUnivRoleExist)){
			add(new UnivFrag("univRole", this, new CayenneModel<UserRoles>(ur)));
		}else{
			add(new EmptyPanel("univRole"));
		}
		
		CayenneList<UserDeptLink> udll = new CayenneList<UserDeptLink>(deptRoles);
			
		add(
				new ListView<IModel<UserDeptLink>>("roleList", udll){

					@Override
					protected void populateItem(ListItem<IModel<UserDeptLink>> item) {
						UserDeptLink deptRole = item.getModelObject().getObject();
						if(deptRole.getRoleType() != UserDeptLink.STUDENT){
							item.add(new DeptFrag("roleItem", UserRolePanel.this, item.getModelObject()));
						}else{
							item.add(new BatchFrag("roleItem", UserRolePanel.this, item.getModelObject()));
						}
						if(deptRole.getStatus()==UserDeptLink.STATUS_INVITED){
							item.setVisible(false);
						}
					}
					
				}
		);
	}
	
	public class UnivFrag extends Fragment{

		public UnivFrag(String id, MarkupContainer markupProvider, IModel<UserRoles> model) {
			super(id, "univFrag", markupProvider, model);
			add(new Label("roleUF", UserRoles.getRoleAsString(model.getObject().getRoleType())));
			add(new EduShareURLPanel("univLinkUF", new CayenneModel<Universities>(model.getObject().getUniversity())));
		}
		
	}
	
	public class DeptFrag extends Fragment{

		public DeptFrag(String id, MarkupContainer markupProvider, IModel<UserDeptLink> model) {
			super(id, "deptFrag", markupProvider, model);
			Departments d = model.getObject().getDepartmentLink();
			Universities u = (d.getUnivLinks()!=null)?d.getUnivLinks().get(0).getUniversity():null;
			
			add(new Label("roleDF", UserDeptLink.getRoleAsString(model.getObject().getRoleType())));
			add(new EduShareURLPanel("deptLinkDF", new CayenneModel<Departments>(d)));
			add(new EduShareURLPanel("univLinkDF", new CayenneModel<Universities>(u)));
		}
		
	}
	
	public class BatchFrag extends Fragment{

		public BatchFrag(String id, MarkupContainer markupProvider, IModel<UserDeptLink> model) {
			super(id, "batchFrag", markupProvider, model);
			Departments d = model.getObject().getDepartmentLink();
			Universities u = (d.getUnivLinks()!=null)?d.getUnivLinks().get(0).getUniversity():null;
			
			add(new Label("roleBF", UserDeptLink.getRoleAsString(model.getObject().getRoleType())));
			add(new EduShareURLPanel("deptLinkBF", new CayenneModel<Departments>(d)));
			add(new EduShareURLPanel("univLinkBF", new CayenneModel<Universities>(u)));
			
			List<UserBatchLink> ubls = userModel.getObject().getUserBatchLink();
			String batchesStr = "";
			for(UserBatchLink ubl:ubls){
				if(ubl.getBatch().getCurricula().getDepartment().getObjectId().equals(d.getObjectId())){
					batchesStr = batchesStr+ubl.getBatch().getBatchName()+", ";
				}
			}
			if(batchesStr.endsWith(", ")){
				batchesStr = batchesStr.substring(0, batchesStr.length()-2);
			}
			
			add(new Label("batchOf", batchesStr));
		}
		
	}

}
